package com.dxy.bizapigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

import java.util.Random;

/**
 * Created by cxf_dxy on 2018/8/21.
 * 权限校验过滤器
 */
public class AuthFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }//路由前执行

    @Override
    public int filterOrder() {
        return 0;
    } //执行顺序 从小到大执行

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        //是否需要执行该过滤器,可以根据前一个过滤器判断是否需要执行
        if(ctx.get("AuthFilter") == null || ctx.get("AuthFilter").equals("Y"))
            return true;
        return false;
    }

    @Override
    public Object run() throws ZuulException {

//        RequestContext ctx = RequestContext.getCurrentContext();
//
//        System.out.println("TODO Some Thing");
//        //TODO call biz sso or dxy sso service
//        if(new Random(10).nextInt() < 5){
//            ctx.setResponseStatusCode(401);
//            ctx.setResponseBody("未授权");
//        }
        return null;
    }
}
